package com.zlx.sharelive.activity;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.zlx.module_base.base_ac.BaseAc;
import com.zlx.module_base.base_fg.BaseFg;
import com.zlx.sharelive.R;
import com.zlx.sharelive.fragment.AuctionHallFg;
import com.zlx.sharelive.fragment.AuctionPartnerFg;
import com.zlx.sharelive.fragment.BiddingResultsFg;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 拍卖伴侣
 */
public class AuctionAc extends BaseAc {

    @BindView(R.id.iv_type1)
    ImageView iv_type1;
    @BindView(R.id.iv_type2)
    ImageView iv_type2;
    @BindView(R.id.iv_type3)
    ImageView iv_type3;
    @BindView(R.id.tv_type1)
    TextView tv_type1;
    @BindView(R.id.tv_type2)
    TextView tv_type2;
    @BindView(R.id.tv_type3)
    TextView tv_type3;

    private Integer[] mImageInteger1 = {R.mipmap.auction_1_1, R.mipmap.auction_2_1, R.mipmap.auction_3_1};
    private Integer[] mImageInteger2 = {R.mipmap.auction_1_2, R.mipmap.auction_2_2, R.mipmap.auction_3_2};
    private List<TextView> mTextViewList = new ArrayList<>();
    private List<ImageView> mViewList = new ArrayList<>();
    private List<BaseFg> mFragment = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.ac_auction;
    }

    @Override
    public void initViews() {
        super.initViews();


        mTextViewList.clear();
        mTextViewList.add(tv_type1);
        mTextViewList.add(tv_type2);
        mTextViewList.add(tv_type3);
        mViewList.clear();
        mViewList.add(iv_type1);
        mViewList.add(iv_type2);
        mViewList.add(iv_type3);
        initFragment();
    }


    private void initFragment() {
        mFragment.clear();
        mFragment.add(new AuctionPartnerFg());
        mFragment.add(new AuctionHallFg());
        mFragment.add(new BiddingResultsFg());
        commitAllowingStateLoss(0);
    }

    private void commitAllowingStateLoss(int position) {
        hideAllFragment();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        Fragment currentFragment = getSupportFragmentManager().findFragmentByTag(position + "");
        if (currentFragment != null) {
            transaction.show(currentFragment);
        } else {
            currentFragment = mFragment.get(position);
            transaction.add(R.id.frameLayout, currentFragment, position + "");
        }
        transaction.commitAllowingStateLoss();
    }

    /**
     * 隐藏所有Fragment
     */
    private void hideAllFragment() {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        for (int i = 0; i < mFragment.size(); i++) {
            Fragment currentFragment = getSupportFragmentManager().findFragmentByTag(i + "");
            if (currentFragment != null) {
                transaction.hide(currentFragment);
            }
        }
        transaction.commitAllowingStateLoss();
    }

    /**
     * 关闭该页面
     */
    public void onFinish() {
        finish();
    }


    @OnClick({R.id.ll_auction1, R.id.ll_auction2, R.id.ll_auction3})
    public void setOnClick(View view) {
        switch (view.getId()) {
            case R.id.ll_auction1:
                commitAllowingStateLoss(0);
                setTextViewColor(0);
                break;
            case R.id.ll_auction2:
                commitAllowingStateLoss(1);
                setTextViewColor(1);
                break;
            case R.id.ll_auction3:
                commitAllowingStateLoss(2);
                setTextViewColor(2);
                break;
        }
    }


    public void setTextViewColor(int position) {
        for (int i = 0; i < mTextViewList.size(); i++) {
            if (i == position) {
                mTextViewList.get(position).setTextColor(getResources().getColor(R.color.hot_pink));
                mViewList.get(position).setImageResource(mImageInteger1[position]);
            } else {
                mTextViewList.get(i).setTextColor(getResources().getColor(R.color.martini));
                mViewList.get(i).setImageResource(mImageInteger2[position]);
            }
        }
    }

}
